package com.game.ad.xiaom;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.game.ad.AdConstants;
import com.game.ad.callback.AdCallBack;
import com.miui.zeus.mimo.sdk.MimoSdk;
import com.miui.zeus.mimo.sdk.ad.AdWorkerFactory;
import com.miui.zeus.mimo.sdk.ad.IAdWorker;
import com.miui.zeus.mimo.sdk.ad.IRewardVideoAdWorker;
import com.miui.zeus.mimo.sdk.api.IMimoSdkListener;
import com.miui.zeus.mimo.sdk.listener.MimoAdListener;
import com.miui.zeus.mimo.sdk.listener.MimoRewardVideoListener;
import com.xiaomi.ad.common.pojo.AdType;

/**
 * Created by Su on 2019/6/19.
 */
public class XiaoMiMgr {
    private static final String TAG = "Log_XM";

    public static void initXiaoM(Context context) {
        MimoSdk.setEnableUpdate(true); // 开启自升级
        // Todo 正式上线时候务必关闭stage和debug
        MimoSdk.setDebugOn();
        MimoSdk.setStageOn();

        MimoSdk.init(context, AdConstants.APP_ID, AdConstants.APP_KEY, AdConstants.APP_TOKEN, new IMimoSdkListener() {
            @Override
            public void onSdkInitSuccess() {
                Log.d(TAG, "onSdkInitSuccess");
            }

            @Override
            public void onSdkInitFailed() {
                Log.d(TAG, "onSdkInitFailed");
            }
        });
    }

    // =========================================== 开屏 ====================================
    private static IAdWorker mWorker;

    public static void showSplashAd(Context context, ViewGroup container, AdCallBack callBack) {
        try {
            mWorker = AdWorkerFactory.getAdWorker(context, container, new MimoAdListener() {
                @Override
                public void onAdPresent() {
                    Log.d(TAG, "onAdPresent");
                }

                @Override
                public void onAdClick() {
                    Log.d(TAG, "onAdClick");
                }

                @Override
                public void onAdDismissed() {
                    //这个方法被调用时，表示从开屏广告消失。
                    Log.d(TAG, "onAdDismissed");
                    if (callBack != null) {
                        callBack.onAdDismissed();
                    }
                }

                @Override
                public void onAdFailed(String s) {
                    Log.e(TAG, "ad fail message : " + s);
                    if (callBack != null) {
                        callBack.onAdFailed();
                    }
                }

                @Override
                public void onAdLoaded(int size) {
                }

                @Override
                public void onStimulateSuccess() {
                }
            }, AdType.AD_SPLASH);
            mWorker.loadAndShow(AdConstants.PORTRAIT_SPLASH_ID);
        } catch (Exception e) {
            e.printStackTrace();
            container.setVisibility(View.GONE);
        }
    }

    public static void recycleSplash() {
        try {
            mWorker.recycle();
        } catch (Exception e) {
        }
    }

    // =========================================== 横幅 ====================================
    private static IAdWorker mBannerAd;

    public static void showBanner(Context context, ViewGroup container) {
        try {
            mBannerAd = AdWorkerFactory.getAdWorker(context, container, new MimoAdListener() {
                @Override
                public void onAdPresent() {
                    Log.e(TAG, "onAdPresent");
                }

                @Override
                public void onAdClick() {
                    Log.e(TAG, "onAdClick");
                }

                @Override
                public void onAdDismissed() {
                }

                @Override
                public void onAdFailed(String s) {
                    Log.d(TAG, "onAdFailed: " + s);
                }

                @Override
                public void onAdLoaded(int size) {
                }

                @Override
                public void onStimulateSuccess() {
                }
            }, AdType.AD_BANNER);
            mBannerAd.loadAndShow(AdConstants.BANNER_POS_ID);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void recycleBanner() {
        try {
            mBannerAd.recycle();
        } catch (Exception e) {
        }
    }

    // =========================================== 插屏 ====================================
    private static IAdWorker mAdWorker;

    public static void showInterstitial(Activity activity) {
        try {
            mAdWorker = AdWorkerFactory.getAdWorker(activity, (ViewGroup) activity.getWindow().getDecorView(), new MimoAdListener() {
                @Override
                public void onAdPresent() {
                    Log.e(TAG, "onAdPresent");
                }

                @Override
                public void onAdClick() {
                    Log.e(TAG, "onAdClick");
                }

                @Override
                public void onAdDismissed() {
                    Log.e(TAG, "onAdDismissed");
                }

                @Override
                public void onAdFailed(String s) {
                    Log.e(TAG, "onAdFailed");
                }

                @Override
                public void onAdLoaded(int size) {
                    Log.e(TAG, "ad loaded");
                    try {
                        mAdWorker.show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onStimulateSuccess() {
                }
            }, AdType.AD_INTERSTITIAL);

            if (!mAdWorker.isReady()) {
                mAdWorker.load(AdConstants.POSITION_INTERSTITIAL_ID);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void recycleInterstitial() {
        try {
            mAdWorker.recycle();
        } catch (Exception e) {
        }
    }


    // =========================================== 激励视频 ====================================
    private static IRewardVideoAdWorker mPortraitVideoAdWorker;

    public static void showRewardVideo(Context context) {
        try {
            mPortraitVideoAdWorker = AdWorkerFactory
                    .getRewardVideoAdWorker(context, AdConstants.PORTRAIT_REWARD_ID, AdType.AD_REWARDED_VIDEO);
            mPortraitVideoAdWorker.setListener(rewardVideoListener);

            // 加载广告
            mPortraitVideoAdWorker.recycle();
            if (!mPortraitVideoAdWorker.isReady()) {
                mPortraitVideoAdWorker.load();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static MimoRewardVideoListener rewardVideoListener = new MimoRewardVideoListener() {
        @Override
        public void onVideoStart() {
            Log.d(TAG, "onVideoStart");
        }

        @Override
        public void onVideoPause() {
            Log.d(TAG, "onVideoPause");
        }

        @Override
        public void onVideoComplete() {
            Log.d(TAG, "onVideoComplete");
        }

        @Override
        public void onAdPresent() {
            Log.d(TAG, "onAdPresent");
        }

        @Override
        public void onAdClick() {
            Log.d(TAG, "onAdClick");
        }

        @Override
        public void onAdDismissed() {
            Log.d(TAG, "onAdDismissed");
        }

        @Override
        public void onAdFailed(String s) {
            Log.d(TAG, "onAdFailed： " + s);
        }

        @Override
        public void onAdLoaded(int i) {
            Log.d(TAG, "onAdLoaded");
            // 加载成功后显示广告
            try {
                mPortraitVideoAdWorker.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onStimulateSuccess() {
            Log.d(TAG, "onStimulateSuccess");
        }
    };
}
